package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.IconAdapter;
import flc.ast.bean.IconBean;
import flc.ast.bean.MyDayBean;
import flc.ast.databinding.ActivityAddCountdownBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;

public class AddCountdownActivity extends BaseAc<ActivityAddCountdownBinding> {

    public static MyDayBean myBean = null;
    private IconAdapter iconAdapter;
    private int oldPosition;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
    private int selRes;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_countdown;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAddCountdownBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvComplete.setOnClickListener(this);

        mDataBinding.rvIconList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        iconAdapter = new IconAdapter();
        mDataBinding.rvIconList.setAdapter(iconAdapter);
        iconAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvComplete://完成
                if (myBean == null) {
                    saveCountdown();
                } else {
                    updateCountdown();
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        iconAdapter.getItem(oldPosition).setSelected(false);
        iconAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        iconAdapter.getItem(position).setSelected(true);
        iconAdapter.notifyItemChanged(position);
        selRes=iconAdapter.getItem(position).getImg();
    }

    @Override
    protected void initData() {
        List<IconBean> listIcon = new ArrayList<>();
        listIcon.add(new IconBean(R.drawable.icon1));
        listIcon.add(new IconBean(R.drawable.icon2));
        listIcon.add(new IconBean(R.drawable.icon3));
        listIcon.add(new IconBean(R.drawable.icon4));
        listIcon.add(new IconBean(R.drawable.icon5));
        listIcon.add(new IconBean(R.drawable.icon6));
        listIcon.add(new IconBean(R.drawable.icon7));
        listIcon.add(new IconBean(R.drawable.icon8));
        listIcon.add(new IconBean(R.drawable.icon9));
        listIcon.add(new IconBean(R.drawable.icon10));
        iconAdapter.setList(listIcon);
        //日前相关
        setDateView();
        String date = "";
        if (myBean == null) {
            date = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
        } else {
            mDataBinding.etName.setText(myBean.getTitle());
            mDataBinding.etDescription.setText(myBean.getContent());
            long millis = TimeUtils.string2Millis(myBean.getDate(), new SimpleDateFormat("yyyy-MM-dd"));
            date = TimeUtils.millis2String(millis, new SimpleDateFormat("yyyy-MM-dd"));
            selRes = myBean.getIcon();
        }
        //滚动到对应日期
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);//年
        int moon = Integer.parseInt(split[1]);//月
        int day = Integer.parseInt(split[2]);//日
        mDataBinding.wheelView1.setCurrentItem(year - 1980);
        mDataBinding.wheelView2.setCurrentItem(moon - 1);
        mDataBinding.wheelView3.setCurrentItem(day - 1);
    }

    //设置日前选择布局
    private void setDateView() {
        mDataBinding.wheelView1.setDividerColor(Color.parseColor("#63D8D8D8"));
        mDataBinding.wheelView1.setTextColorCenter(Color.parseColor("#0095FF"));
        mDataBinding.wheelView1.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView1.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView1.setCurrentItem(43);
        mDataBinding.wheelView1.setItemsVisibleCount(5);
        mDataBinding.wheelView1.setCyclic(true);
        mDataBinding.wheelView2.setDividerColor(Color.parseColor("#63D8D8D8"));
        mDataBinding.wheelView2.setTextColorCenter(Color.parseColor("#0095FF"));
        mDataBinding.wheelView2.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView2.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView2.setCurrentItem(0);
        mDataBinding.wheelView2.setItemsVisibleCount(5);
        mDataBinding.wheelView2.setCyclic(true);
        mDataBinding.wheelView3.setDividerColor(Color.parseColor("#63D8D8D8"));
        mDataBinding.wheelView3.setTextColorCenter(Color.parseColor("#0095FF"));
        mDataBinding.wheelView3.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView3.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView3.setCurrentItem(0);
        mDataBinding.wheelView3.setItemsVisibleCount(5);
        mDataBinding.wheelView3.setCyclic(true);
        //设置滚轮数据
        List<String> listYear = new ArrayList<>();//年 1980-2040
        for (int i = 0; i < 61; i++) {
            listYear.add(String.valueOf(1980 + i));
        }
        List<String> listMonth = new ArrayList<>();//月 12
        for (int i = 0; i < 12; i++) {
            listMonth.add(String.valueOf((i + 1) < 10 ? "0" + (i + 1) : i + 1));
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listYear);
        mDataBinding.wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMonth);
        mDataBinding.wheelView2.setAdapter(adapter2);
        setDayDate(31);//日 默认显示1月 共31天
        //监听月份滚动选择
        mDataBinding.wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                index = index + 1;
                if (index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 || index == 12) {
                    //大月31天
                    setDayDate(31);
                } else if (index == 2) {
                    //2月判断是闰(29)还是平(28)
                    if (TimeUtils.isLeapYear(TimeUtils.getNowString(format), format)) {
                        setDayDate(29);
                    } else {
                        setDayDate(28);
                    }
                } else {
                    //小月30天
                    setDayDate(30);
                }
            }
        });
    }

    //设置天
    private void setDayDate(int num) {
        List<String> listDay = new ArrayList<>();//日 默认显示1月31天
        for (int i = 0; i < num; i++) {
            listDay.add(String.valueOf((i + 1) < 10 ? "0" + (i + 1) : i + 1));
        }
        WheelAdapter adapter3 = new ArrayWheelAdapter(listDay);
        mDataBinding.wheelView3.setAdapter(adapter3);
    }

    //保存倒计时
    private void saveCountdown() {
        //标题
        String title = mDataBinding.etName.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShort(R.string.please_input_title);
            return;
        }
        //内容
        String content = mDataBinding.etDescription.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(R.string.please_input_content_tip);
            return;
        }
        //tup
        if (selRes == 0) {
            ToastUtils.showShort(R.string.please_add_img);
            return;
        }
        showDialog(getString(R.string.saving));
        //选择的日期
        int year = mDataBinding.wheelView1.getCurrentItem() + 1980;
        int moon = mDataBinding.wheelView2.getCurrentItem() + 1;
        int day = mDataBinding.wheelView3.getCurrentItem() + 1;
        String date = year + "-" + moon + "-" + day;
        //准备数据
        List<MyDayBean> listSave = new ArrayList<>();
        listSave.add(new MyDayBean(TimeUtils.getNowString(), title, content, date, "", selRes));
        List<MyDayBean> listSp = AppSpUtils.getCountdownList();
        if (listSp != null && listSp.size() != 0) {
            listSp.addAll(listSave);
            AppSpUtils.savCountdownList(listSp);
        } else {
            AppSpUtils.savCountdownList(listSave);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                finish();
            }
        }, 500);
    }

    //修改倒计时
    private void updateCountdown() {
        //标题
        String title = mDataBinding.etName.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShort(R.string.please_input_title);
            return;
        }
        //内容
        String content = mDataBinding.etDescription.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(R.string.please_input_content_tip);
            return;
        }
        //背景
        if (selRes == 0) {
            ToastUtils.showShort(R.string.please_add_img);
            return;
        }
        showDialog(getString(R.string.saving));
        //选择的日期
        int year = mDataBinding.wheelView1.getCurrentItem() + 1980;
        int moon = mDataBinding.wheelView2.getCurrentItem() + 1;
        int day = mDataBinding.wheelView3.getCurrentItem() + 1;
        String date = year + "-" + moon + "-" + day;
        //保存数据
        List<MyDayBean> listSp = AppSpUtils.getCountdownList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(myBean.getId())) {
                    listSp.get(i).setTitle(title);
                    listSp.get(i).setContent(content);
                    listSp.get(i).setDate(date);
                    listSp.get(i).setIcon(selRes);
                    break;
                }
            }
            AppSpUtils.savCountdownList(listSp);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                finish();
            }
        }, 500);
    }

}